package com.xbongbong.sso.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.sso.domain.entity.OauthConnectorEntity;
import com.xbongbong.sso.domain.entity.OauthCustomParamEntity;
import com.xbongbong.sso.domain.entity.OauthParamEntity;
import com.xbongbong.sso.enums.PositionEnum;
import com.xbongbong.sso.enums.StageTypeEnum;
import com.xbongbong.sso.pojo.CustomParameterMapPojo;
import com.xbongbong.sso.pojo.OauthParamPojo;
import com.xbongbong.sso.pojo.ParameterMapPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 授权参数帮助类
 */
@Component
public class OauthParamHelp {

    private static final Logger LOG = LoggerFactory.getLogger(OauthParamHelp.class);

    @Resource
    private OauthConnectorHelp oauthConnectorHelp;
    @Resource
    private OauthCustomParamHelp oauthCustomParamHelp;

    public OauthParamPojo oauthParamPojoFormat(OauthParamEntity oauthParam) {
        if (Objects.isNull(oauthParam)) {
            return new OauthParamPojo();
        }
        OauthParamPojo oauthParamPojo = new OauthParamPojo();
        oauthParamPojo.setId(oauthParam.getId());
        oauthParamPojo.setCorpid(oauthParam.getCorpid());
        oauthParamPojo.setConnectorId(oauthParam.getConnectorId());
        oauthParamPojo.setStepId(oauthParam.getStepId());
        JSONObject stageTypeJson = new JSONObject();
        stageTypeJson.put("value", oauthParam.getStageType());
        stageTypeJson.put("text", StageTypeEnum.getByCode(oauthParam.getStageType()).getName());
        oauthParamPojo.setStageType(stageTypeJson);
        Integer position = oauthParam.getPosition();
        if (Objects.nonNull(position)) {
            JSONObject positionJson = new JSONObject();
            positionJson.put("value", position);
            positionJson.put("text", PositionEnum.getByCode(position).getName());
            oauthParamPojo.setPosition(positionJson);
        }
        oauthParamPojo.setAttr(oauthParam.getAttr());
        oauthParamPojo.setStandardAttr(oauthParam.getStandardAttr());

        JSONObject enableJson = new JSONObject();
        enableJson.put("value", oauthParam.getEnable());
        enableJson.put("text", EnableEnum.getByCode(oauthParam.getEnable()).getMemo());
        oauthParamPojo.setEnable(enableJson);
        oauthParamPojo.setDescription(oauthParam.getDescription());
        oauthParamPojo.setUpdateTime(oauthParam.getUpdateTime());
        oauthParamPojo.setAddTime(oauthParam.getAddTime());
        oauthParamPojo.setDel(oauthParam.getDel());
        return oauthParamPojo;
    }

    public ParameterMapPojo getParameterMap(OauthConnectorEntity oauthConnector){
        Map<String, String> sysParameters = oauthConnectorHelp.initSysParameter(oauthConnector);
        CustomParameterMapPojo customParameterMapPojo = oauthCustomParamHelp.getCustomParameterMap(oauthConnector.getCorpid(),
                oauthConnector.getId());
        Map<String, String> constantCustomParameter = customParameterMapPojo.getConstantCustomParameter();
        Map<String, OauthCustomParamEntity> calculationCustomParameterMap = customParameterMapPojo.getCalculationCustomParameterMap();
        Map<String, String> constantParameterMap = new HashMap<>();
        constantParameterMap.putAll(sysParameters);
        constantParameterMap.putAll(constantCustomParameter);
        ParameterMapPojo parameterMapPojo = new ParameterMapPojo();
        parameterMapPojo.setConstantParameterMap(constantParameterMap);
        parameterMapPojo.setCalculationCustomParameterMap(calculationCustomParameterMap);
        return parameterMapPojo;
    }
}
